<!DOCTYPE html>
<html>
<head>
    <title>聊天测试</title>
    <meta charset="utf-8">
</head>
<body>
<div>
    <input type="button" value="1.聊天示例（消息体待完善）" onclick="chat()"/>
    <!--<input type="button" value="2.鉴权请求示例(此示例可直走4.登录请求示例)" onclick="auth()"/>-->
    <!--<input type="button" value="3.握手请求示例" onclick="_handshake()"/>-->
    <input type="button" value="4.登录请求示例" onclick="login()"/>
    <input type="button" value="5.心跳请求示例" onclick="heartbeat()"/>
    <input type="button" value="6.用户退出示例" onclick="user_close()"/>
    <input type="button" value="7.获取用户信息示例" onclick="obtain_user_info()"/>
    <input type="button" value="8.获取用户聊天记录示例（消息体待完善）" onclick="message()"/>
    <input type="button" value="9.添加好友申请消息体示例" onclick="user_apply_handler()"/>
    <input type="button" value="10.获取用户好友申请列表示例" onclick="obtain_user_apply_info()"/>
    <!--<input type="button" value="9.自定义消息体示例" onclick="costom_handler()"/>-->
</div>
<div id="messages"></div>
<script type="text/javascript">
    var webSocket = new WebSocket('ws://192.168.1.43:6789');
    webSocket.onerror = function (event) {
        alert(event.data);
    };
    //与WebSocket建立连接
    // webSocket.onopen = function (event) {
    //     alert("onopen:与WebSocket建立连接了"+event.data);
    //     document.getElementById('messages').innerHTML = '握手成功';
    // };
    //处理服务器返回的信息
    webSocket.onmessage = function (event) {
        alert("onmessage:处理服务器返回的信息开始" + event.data);
        document.getElementById('messages').innerHTML += '<br />' + event.data;
    };

    function login() {
        var login = "{" +
            "\"cmd\":\"5\"," +
            // "\"loginname\":\"zhangsan\"," +
            // "\"password\":\"密码\"," +
            "\"token\":\"2e37b8b3596d476da66d140ea33d1d48\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + login);
        webSocket.send(login);
    }

    function obtain_user_info() {
        // 7.获取用户信息请求消息结构
// 请求:COMMAND_GET_USER_REQ(17)
// 响应:COMMAND_GET_USER_RESP(18)
        var obtain_user_info = "{" +
            "\"cmd\":\"17\"," +
            "\"userid\":\"12\"," +
            "\"type\":\"2\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + obtain_user_info);
        webSocket.send(obtain_user_info);
    }

    function user_apply_handler() {
// 自定义的处理器,即消息体支持自己构建
        var handler = "{" +
            "\"cmd\":\"21\"," +
            "\"appliedUserId\":\"11\"," +
            "\"applyUserId\":\"12\"," +
            "\"applicationInformation\":\"申请信息\"" +
            "}";

        //向服务器发送请求
        alert("send:向服务器发送请求:" + handler);
        webSocket.send(handler);
    }
    function obtain_user_apply_info() {
// 自定义的处理器,即消息体支持自己构建
        var handler = "{" +
            "\"cmd\":\"23\"" +
            "}";

        //向服务器发送请求
        alert("send:向服务器发送请求:" + handler);
        webSocket.send(handler);
    }

    function auth() {
        var auth = "{" +
            "\"cmd\":\"3\"," +
            "\"token\": \"校验码\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + auth);
        webSocket.send(auth);
    }


    function user_close() {
        // 6.关闭、退出请求消息结构
// 请求:COMMAND_CLOSE_REQ(14)
// 响应:无
        var close = "{" +
            // "\"cmd\":\"命令码(14)int类型\"," +
            "\"cmd\":\"14\"," +
            // "\"userid\":\"用户id\"" +
            "\"userid\":\"17\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + close);
        webSocket.send(close);
    }

    function _handshake() {
        // 3.握手请求消息结构
// 说明:请求:COMMAND_HANDSHAKE_REQ(1)
// 响应:COMMAND_HANDSHAKE_RESP(2)
        var handshake = "{" +
            "\"cmd\":\"1\"," +
            "\"hbyte\":\"8\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + handshake);
        webSocket.send(handshake);
    }
    function heartbeat() {
        var heartbeat = "{" +
            // "\"cmd\":\"命令码(13)int类型\"," +
            "\"cmd\":\"13\"," +
            // "\"hbbyte\":\"心跳1个字节\"" +
            "\"hbbyte\":\"a\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + heartbeat);
        webSocket.send(heartbeat);
    }
    function message() {
// 8.获取用户消息请求结构
// 请求:COMMAND_GET_MESSAGE_REQ(19)
// 响应:COMMAND_GET_MESSAGE_RESP(20)
        var message = "{" +
            // "\"cmd\":\"命令码(19)int类型\"," +
            "\"cmd\":\"19\"," +
            "\"fromUserId\":\"消息发送用户id(此字段必须与userId一起使用,获取双方聊天消息),非必填\"," +
            "\"userId\":\"当前用户id(必填字段),当只有此字段时,type必须为0，意思是获取当前用户所有离线消息(好友+群组)\"," +
            "\"groupId\":\"群组id(此字段必须与userId一起使用,获取当前用户指定群组聊天消息),非必填\"," +
            "\"beginTime\":\"消息区间开始时间Date毫秒数double类型,非必填\"," +
            "\"endTime\":\"消息区间结束时间Date毫秒数double类型,非必填\"," +
            "\"offset\":\"分页偏移量int类型，类似Limit 0,10 中的0,非必填\"," +
            "\"count\":\"显示消息数量,类似Limit 0,10 中的10,非必填\"," +
            "\"type\":\"消息类型(0:离线消息,1:历史消息)\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + message);
        webSocket.send(message);
    }
    function chat() {
        // 1.聊天请求消息结构
// 请求:COMMAND_CHAT_REQ(11)
// 响应:COMMAND_CHAT_RESP(12)
        var chat = "{" +
            "\"from\": \"来源ID\"," +
            "\"to\": \"目标ID\"," +
            "\"cmd\":\"11\"," +
            "\"createTime\": \"消息创建时间long类型\"," +
            "\"msgType\": \"消息类型int类型(0:text、1:image、2:voice、3:vedio、4:music、5:news)\"," +
            "\"chatType\":\"聊天类型int类型(0:未知,1:公聊,2:私聊)\"," +
            "\"group_id\":\"群组id仅在chatType为(1)时需要,String类型\"," +
            "\"content\": \"内容\"" +
            "}";
        //向服务器发送请求
        alert("send:向服务器发送请求:" + chat);
        webSocket.send(chat);
    }

</script>
</body>
</html>
